package com.yundian.shop.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.yundian.dao.BaseDAO;
import com.yundian.shop.dao.IShopDAO;
import com.yundian.vo.Shop;

public class ShopDAO extends BaseDAO implements IShopDAO {

	@Override
	public List browseShop() {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "from Shop as m";
		Query query = session.createQuery(hql);
		List<Shop> list = query.list();
		tx.commit();
		session.close();
	 return list;
	}
	@Override
	public Shop getShop(Integer shopid) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		Shop shop = (Shop) session.get(Shop.class, shopid);
		tx.commit();
		session.close();
		return shop;
	}

	@Override
	public Shop loadShop(Integer shopid) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		Shop shop = (Shop) session.load(Shop.class, shopid);
		tx.commit();
		session.close();
		return shop;
	}

	@Override
	public void saveShop(Shop shop) {
		// TODO Auto-generated method stub
		Session session=getSession();
		Transaction tx=session.beginTransaction();
		session.save(shop);
		tx.commit();
		session.close();
		

	}

	@Override
	public Shop searchShopByshopName(String shopname) {
		// TODO Auto-generated method stub
	    Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "from Shop as s where s.shopName=:shopname";
		Query query = session.createQuery(hql);
		query.setString("shopname", shopname);
		List<Shop> list = query.list();
		tx.commit();
		session.close();
		if(list.size()==1){
			return list.get(0);
		}
		return null;
	}
	@Override
	public Shop searchShopByshopShowName(String shopShowname) {
		// TODO Auto-generated method stub
	    Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "from Shop as s where s.shopShowName=:shopShowname";
		Query query = session.createQuery(hql);
		query.setString("shopShowname", shopShowname);
		List<Shop> list = query.list();
		tx.commit();
		session.close();
		if(list.size()==1){
			return list.get(0);
		}
		return null;
	}
	@Override
	public void updateShop(Shop shop) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		Session session=getSession();
		Transaction tx=session.getTransaction();
		session.update(shop);
		tx.commit();
		session.close();
	}
	@Override
	public Shop searchShopByEmail(String email) {
		 Session session = getSession();
			Transaction tx = session.beginTransaction();
			String hql = "from Shop as s where s.shopEmail=:email";
			Query query = session.createQuery(hql);
			query.setString("email", email);
			List<Shop> list = query.list();
			tx.commit();
			session.close();
			if(list.size()==1){
				return list.get(0);
			}
			return null;
	}

}
